package tk.mybatis.mapper.mapperhelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import tk.mybatis.mapper.entity.EntityColumn;
import tk.mybatis.mapper.entity.EntityTable;
import tk.mybatis.mapper.entity.NestdEntity;
import tk.mybatis.mapper.entity.NestdEntityContext;
import tk.mybatis.mapper.util.StringUtil;


/**
 * 
 * title: NestdEntityHelper.java 
 * description
 *
 * @author rplees
 * @email rplees.i.ly@gmail.com
 * @version 1.0  
 * @created Jul 3, 2016 10:14:17 PM
 */
public final class ExampleValidator {
	EntityTable entityTable;
	MetaObject msObject;
	//属性和列名
	Map<String, String> propertyMap;
	
	public ExampleValidator(Class<?> clazz) {
		entityTable = EntityHelper.getEntityTable(clazz);
		msObject = SystemMetaObject.forObject(clazz);
		
		propertyMap = new HashMap<String, String>(entityTable.getEntityClassColumns().size());
		NestdEntityContext context = new NestdEntityContext(clazz);
		_doOne(entityTable, propertyMap, context);
		
		if(entityTable.hasNestedTableSelect()) {
			List<NestdEntity> nestdEntitys = entityTable.getNestdEntitys();
			for (NestdEntity nestdEntity : nestdEntitys) {
				context.clear();
				context.record(nestdEntity);
				_doOneNested(nestdEntity, propertyMap, context);
			}
		}
	}
	
	protected static void _doOneNested(NestdEntity nestdEntity, Map<String, String> propertyMap, NestdEntityContext context) {
		EntityTable entityTable = nestdEntity.getEntityTable();
		_doOne(entityTable, propertyMap, context);
		
		if(entityTable.hasNestedTableSelect()) {
			List<NestdEntity> childNestdEntitys = entityTable.getNestdEntitys();
			for (NestdEntity childNestdEntity : childNestdEntitys) {
				context.record(childNestdEntity);
				_doOneNested(childNestdEntity, propertyMap, context);
			}
		}
	}
	
	protected static void _doOne(EntityTable entityTable, Map<String, String> propertyMap, NestdEntityContext context) {
		for (EntityColumn column : entityTable.getEntityClassColumns()) {
			if(context.getOGNLTestSuffix() == null && !entityTable.hasNestedTableSelect()) {
				 propertyMap.put(column.getProperty(), column.getColumn());
			} else {
				String propertyOGNLTestSuffix = context.getOGNLTestSuffix();
				if(propertyOGNLTestSuffix != null) {
					propertyOGNLTestSuffix += ".";
				} else {
					propertyOGNLTestSuffix = "";
				}
				
				propertyOGNLTestSuffix = StringUtils.replace(propertyOGNLTestSuffix, "[0]", "");
				propertyMap.put(propertyOGNLTestSuffix + column.getProperty(), SqlNestdHelper.doKeyword(context.getAliasSuffix()) + "." + column.getColumn());
			}
        }
	}
	
	public boolean contains(String property) {
		if(StringUtils.indexOf(property, ".") < 0) {
			return propertyMap.containsKey(property);
		}
		
		String[] split = StringUtil.splitRecursionPlus(property);
		for (String string : split) {
			Object value = msObject.getValue(string);
			if(value == null) {
				return false;
			}
		}
		
		return true;
	}
	public Map<String, String> getPropertyMap() {
		return propertyMap;
	}
}